package hua.mtreader.feature.bookshelf.fragment;

import android.os.Looper;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;

import hua.mtreader.R;
import hua.mtreader.base.BaseFragment;
import hua.mtreader.common.utils.ToastAlone;
import hua.mtreader.common.utils.UIUtils;
import hua.mtreader.feature.bookshelf.adapter.GridBookAdapter;
import hua.mtreader.feature.bookshelf.bean.BookshelfRepsone;
import hua.mtreader.feature.bookshelf.presenter.BookshelfPresenter;
import hua.mtreader.feature.bookshelf.view.BookshelfTopView;
import hua.mtreader.feature.bookshelf.view.IBookshelfView;
import hua.mtreader.feature.bookshelf.view.MyScrollView;

/**
 * Created by Administrator on 2017/3/14.
 */

public class BookshelfFragment extends BaseFragment<IBookshelfView,BookshelfPresenter> implements IBookshelfView {

    private final String TAG = getClass().getSimpleName();
    private MyScrollView svContent;
    private BookshelfTopView rlRencent;
    private RecyclerView rvContent;

    private GridBookAdapter gridBookAdapter;

    private BookshelfRepsone mRepsone;


    @Override
    public BookshelfPresenter createPresenter() {
        return new BookshelfPresenter();
    }

    @Override
    public View initView() {
        return View.inflate(getContext(), R.layout.fragment_bookshelf, null);
    }

    @Override
    public void initData() {
        assignViews();
        getBooks();
    }

    private void getBooks() {
        mPresenter.getBooks();
    }


    @Override
    protected void setListener() {
        svContent.setOnRefreshListener(new MyScrollView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getBooks();
            }
        });
    }


    private void assignViews() {
        svContent = (MyScrollView) view.findViewById(R.id.sv_content);
        rlRencent = (BookshelfTopView) view.findViewById(R.id.rl_rencent);
        rvContent = (RecyclerView) view.findViewById(R.id.rv_content);

        rlRencent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                svContent.setTopViewHeight(rlRencent);
                rlRencent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });

        /*LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        linearLayoutManager.setSmoothScrollbarEnabled(true);
        linearLayoutManager.setAutoMeasureEnabled(true);

        rvContent.setLayoutManager(linearLayoutManager);*/

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        gridLayoutManager.setSmoothScrollbarEnabled(true);
        gridLayoutManager.setAutoMeasureEnabled(true);
        rvContent.setLayoutManager(gridLayoutManager);


        rvContent.setHasFixedSize(true);
        rvContent.setNestedScrollingEnabled(false);

        // rvContent.setAdapter(new ListBookAdapter());
        gridBookAdapter = new GridBookAdapter();
        rvContent.setAdapter(gridBookAdapter);
    }

    @Override
    public void onLoadBooksSuccess(BookshelfRepsone repsone) {
        this.mRepsone = repsone;

        if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
            Log.e(TAG, "onLoadBooksSuccess: Main Thread" );
        } else {
            Log.e(TAG, "onLoadBooksSuccess: Not Main Thread" );
        }

        if (repsone.getResult() == 0) {
            ToastAlone.showShortToast(repsone.getError_msg());
        }else{
            UIUtils.runInMainThread(new Runnable() {
                @Override
                public void run() {
                    gridBookAdapter.setData(mRepsone.getData().getBookList());
                }
            });
        }
    }
}
